<?php

namespace app\client\controller\system;

use app\common\controller\Frontend;
use app\common\enums\AcceptAccountTypeEnum;
use app\common\model\banxi\sys\SysNotification as SysNotificationModel;
use app\common\model\banxi\sys\SysNotificationAccept;

class SysNotification extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 分页-通知公告
     */
    public function listMyNotification()
    {
        $params = $this->request->get(['index', 'limit']);
        $pageParams = get_page_params($params);
        $userInfo = $this->auth->getUserInfo();
        $accountId = $userInfo['id'];
        $accountType = AcceptAccountTypeEnum::STUDENT;
        $SysNotificationModel = new SysNotificationModel();
        $list = $SysNotificationModel->listMyInfoPage($pageParams['page'], $pageParams['limit'], $accountId, $accountType);
        $this->ok('获取成功', result_row($list));
    }

    /**
     * 分页-通知公告详情
     */
    public function getNotificationInfo()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('请选择你要查看的通知公告');
        }
        $userInfo = $this->auth->getUserInfo();
        $accountId = $userInfo['id'];
        $SysNotificationAcceptModel = new SysNotificationAccept();
        $SysNotificationAcceptModel->markRead($id, $accountId, AcceptAccountTypeEnum::STUDENT);
        $SysNotificationModel = new SysNotificationModel();
        $info = $SysNotificationModel->getInfo(['id' => $id]);
        $this->ok('获取成功', $info);
    }

    /**
     * 查询-未读数量
     */
    public function getMyUnreadCount()
    {
        $userInfo = $this->auth->getUserInfo();
        $accountId = $userInfo['id'];
        $accountType = AcceptAccountTypeEnum::STUDENT;
        $SysNotificationModel = new SysNotificationModel();
        $count = $SysNotificationModel->getMyUnreadCount($accountId, $accountType);
        $this->ok('获取成功', $count);
    }
}